package com.test.action;

import java.util.HashSet;
import java.util.List;

import org.apache.commons.codec.digest.DigestUtils;

import com.test.base.BaseAction;
import com.test.domain.Department;
import com.test.domain.Role;
import com.test.domain.User;
import com.test.util.DepartmentUtils;

import com.opensymphony.xwork2.ActionContext;

public class UserAction extends BaseAction<User> {
	private static final long serialVersionUID = 1L;
	private Integer[] roleIds;
	private Integer departmentId;
	
	// ---

	public Integer getDepartmentId() {
		return departmentId;
	}

	public void setDepartmentId(Integer departmentId) {
		this.departmentId = departmentId;
	}

	public Integer[] getRoleIds() {
		return roleIds;
	}

	public void setRoleIds(Integer[] roleIds) {
		this.roleIds = roleIds;
	}
	//已经可以从BaseAction中直接继承得到
	//从容器中注入service
/*	protected UserService userService;
	protected RoleService roleService;
	protected DepartmentService departmentService;
	public UserService getUserService() {
		return userService;
	}
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	public RoleService getRoleService() {
		return roleService;
	}
	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}
	public DepartmentService getDepartmentService() {
		return departmentService;
	}
	public void setDepartmentService(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}*/
	
	
	/** 列表 */
	public String list() throws Exception {
		List<User> userList = userService.findAll();
		System.out.println("这里是list()方法！");
		ActionContext.getContext().put("userList", userList);
		return "list";
	}

	/** 删除 */
	public String delete() throws Exception {
		userService.deleteUser(model.getId());
		return "toList";
	}

	/** 添加页面 */
	public String addUI() throws Exception {
		System.out.println("这里是addUI()方法！");
		// 准备数据：roleList
		List<Role> roleList = roleService.findAll();
		ActionContext.getContext().put("roleList", roleList);
		for(Role role : roleList){
			System.out.println("用户的角色:"+role.getName());
		}
		//准备部门数据
		if(departmentService==null){
			System.out.println("完了，这里的departmentService竟然是空的！");
		}
		List<Department> topList = departmentService.findTopList();
		if(topList!=null){
			System.out.println("fuck,it is null!");
		}
		for(Department dept : topList){
			System.out.println("所有顶级部门为:"+dept);
		}
		List<Department> departmentList = DepartmentUtils.getAllDepartments(topList, null);
		ActionContext.getContext().put("departmentList", departmentList);
		return "saveUI";
	}

	/** 添加 */
	public String add() throws Exception {
		// >> 处理关联的多个岗位
		List<Role> roleList = roleService.findRoleByIds(roleIds);
		model.setRoles(new HashSet<Role>(roleList));
		//处理关联的部门
		Department department = departmentService.findDepartmentById(departmentId);
		model.setDepartment(department);
		// 保存到数据库
		userService.addUser(model);

		return "toList";
	}

	/** 修改页面 */
	public String editUI() throws Exception {
		// 准备回显的数据
		User user = userService.findUserById(model.getId());
		ActionContext.getContext().getValueStack().push(user);
		//准备部门
		List<Department> topList = departmentService.findTopList();
		List<Department> departmentList = DepartmentUtils.getAllDepartments(topList, null);
		ActionContext.getContext().put("departmentList", departmentList);
		// >> 处理岗位
		roleIds = new Integer[user.getRoles().size()];
		int index = 0;
		for (Role role : user.getRoles()) {
			roleIds[index++] = role.getId();
		}

		// 准备数据：roleList
		List<Role> roleList = roleService.findAll();
		ActionContext.getContext().put("roleList", roleList);

		return "saveUI";
	}

	/** 修改 */
	public String edit() throws Exception {
		// 1，从数据库中取出原对象
		User user = userService.findUserById(model.getId());

		// 2，设置要修改的属性
		user.setLoginName(model.getLoginName());
		user.setName(model.getName());
		user.setGender(model.getGender());
		user.setPhoneNumber(model.getPhoneNumber());
		user.setEmail(model.getEmail());
		user.setDescription(model.getDescription());
		// >> 处理关联的多个岗位
		List<Role> roleList = roleService.findRoleByIds(roleIds);
		user.setRoles(new HashSet<Role>(roleList));
		//处理关联的部门
		Department department = departmentService.findDepartmentById(departmentId);
		user.setDepartment(department);
		// 3，更新到数据库
		userService.updateUser(user);

		return "toList";
	}

	/** 初始化密码为1234 */
	public String initPassword() throws Exception {
		// 1，从数据库中取出原对象
		User user = userService.findUserById(model.getId());

		// 2，设置要修改的属性
		String md5 = DigestUtils.md5Hex("1234"); // 密码要使用MD5摘要
		user.setPassword(md5);

		// 3，更新到数据库
		userService.updateUser(user);

		return "toList";
	}

}
